import React,{Component} from 'react';
import Card from '../../../widget/card/card.jsx';
import './companyindex.scss';
import LazyLoad from 'react-lazyload';
import { getShopGoods, getIndexInfo } from 'api/company.js';
import { Carousel } from 'antd';
export default class Componentindex extends Component{
	constructor (props) {
		super(props)
		this.state={
			bannerImgInfo:[],
			current: 1,
			recommendList:[],
			newestList: []
		}
	}
	_getShopGoods (num) {
		getShopGoods({
			shop_id : this.props.shop_id,
			sort: num
		}).then(res => {
			if(res.code === 1){
				if (num === 1) {
					this.setState({
						recommendList:res.data.data
					})
				} else if (num === 2) {
					this.setState({
						newestList:res.data.data
					})
				}
			}
		})
	}
	componentDidMount () {
		this._getShopGoods(1);
		this._getShopGoods(2);
		getIndexInfo({
			shop_id : this.props.shop_id
		}).then(res => {
			if(res.code === 1){
				Object.keys(res.data).forEach(key => {
					if (key === 'bannerImgInfo'){
						this.setState({
							bannerImgInfo:res.data[key]
						})
					}
				})
			}
		})
	}
	render(){
		return(
			<div className="companyindex">
				<Carousel dots={false} autoplay={true} >
					{
						this.state.bannerImgInfo.length === 0 ?
						''
						:
						this.state.bannerImgInfo.map((item, index) => {
						return (
							<img src={item.pic_url} key={index} className="shop_banner"/>
						)
					})}
				</Carousel>
				<div className="product">
					<div className="company_title">
						<span>推荐产品</span>
						<span onClick={() => {this.props.handleSort(1)}}>查看更多></span>
					</div>
					<div className="com_content">
						{
							this.state.recommendList.map((item,index) =>{
								return (
	                <LazyLoad offsetTop={200} key={index} height={0}>
	                  <Card shopDetail={{...item, hide: false}}/>
	                </LazyLoad>
	              )
							})
						}
					</div>
				</div>
				<div className="product">
					<div className="company_title">
						<span>最新产品</span>
						<span onClick={() => {this.props.handleSort(2)}}>查看更多></span>
					</div>
					<div className="com_content">
						{
							this.state.newestList.map((item,index) =>{
								return (
	                <LazyLoad offsetTop={200} key={index} height={0}>
	                  <Card shopDetail={{...item, hide: false}}/>
	                </LazyLoad>
	              )
							})
						}
					</div>
				</div>
			</div>
		)
	}
}
